package com.wayhome.srpingbootmybatis.controller.excel;

import cn.afterturn.easypoi.excel.entity.ExportParams;
import com.alibaba.excel.EasyExcel;
import com.alibaba.excel.write.style.column.SimpleColumnWidthStyleStrategy;
import com.wayhome.srpingbootmybatis.excel.Dr9225AccessInfo;
import com.wayhome.srpingbootmybatis.excel.ali.Order;
import com.wayhome.srpingbootmybatis.excel.ali.Product;
import com.wayhome.srpingbootmybatis.speacecase.HeadContentCellStyle;
import com.wayhome.srpingbootmybatis.utils.ExcelUtil;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.context.request.RequestContextHolder;
import org.springframework.web.context.request.ServletRequestAttributes;

import javax.servlet.http.HttpServletResponse;
import java.math.BigDecimal;
import java.net.URLEncoder;
import java.util.ArrayList;
import java.util.List;
import java.util.Objects;

@RestController
@RequestMapping(value = "/ali/easy_excel")
public class AliEasyExcelController {


    @PostMapping(value = "/export/one_to_many")
    public void exportOneToMany (HttpServletResponse httpServletResponse) throws Exception {
        List<Order> list = new ArrayList<>();
        Order order = new Order();
        order.setOrderNo("130001");
        order.setCustomerName("张旸");


        List<Product> products = new ArrayList<>();
        Product xiaomi = new Product();
        xiaomi.setPrice(new BigDecimal("1399.00"));
        xiaomi.setProductName("红米k80Pro");
        xiaomi.setQuantity(1);

        Product huawei = new Product();
        huawei.setPrice(new BigDecimal("4499.00"));
        huawei.setProductName("华为荣耀X16");
        huawei.setQuantity(1);

        products.add(xiaomi);
        products.add(huawei);

        order.setProducts(products);

        list.add(order);

//        HttpServletResponse response = ((ServletRequestAttributes) Objects.requireNonNull(RequestContextHolder.getRequestAttributes())).getResponse();
//        assert response != null;
//        response.setContentType("application/vnd.openxmlformats-officedocument.spreadsheetml.sheet");
//        response.setCharacterEncoding("utf-8");// 设置字符编码
//        String fileName1 = URLEncoder.encode("2", "UTF-8").replaceAll("\\+", "%20");
//        response.setHeader("Content-disposition", "attachment;filename*=utf-8''" + fileName1 + ".xlsx"); // 设置响应头


        ExcelUtil.exportExcel(list, "用户购买信息表", "购买记录", Order.class,"2.xlsx", httpServletResponse);

    }


}
